//
//  JYJProgramModel.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/1.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJProgramModel.h"
#import "JYJMusicOperation.h"
@implementation JYJProgramModel
// 取值
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        _music_id = [aDecoder decodeIntegerForKey:@"music_id"];
        _music_title = [aDecoder decodeObjectForKey:@"music_title"];
        _music_img = [aDecoder decodeObjectForKey:@"music_img"];
        
    }
    return self;
}
// 赋值  存储
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeInteger:_music_id forKey:@"music_id"];
    [aCoder encodeObject:_music_title forKey:@"music_title"];
    [aCoder encodeObject:_music_img forKey:@"music_img"];
}

- (NSString *)localPath {
//    NSString *pathName = [NSString stringWithFormat:@"/Documents/JYJYMusics/%ld.mp3",self.music_id];
//    NSString *filePath = [NSHomeDirectory() stringByAppendingString:pathName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject] stringByAppendingPathComponent:@"download"];
    NSError *error = nil;
    BOOL isCreateSuccess = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
    if (isCreateSuccess) {
        NSLog(@"创建文件夹成功");
    }else {
        NSLog(@"创建文件夹失败");
    }
    NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.mp3",self.music_id]];
   
    return filePath;
}
- (void)setProgress:(CGFloat)progress {
    if (_progress != progress) {
        _progress = progress;
    if (self.onProgressChanged) {
        self.onProgressChanged(self);
    }else {
        NSLog(@"progress changed block is empty");
    }
  }
}
- (void)setStatus:(JYJMusicStatus)status {
    if (_status != status) {
        _status = status;
        if (self.onStatusChanged) {
            self.onStatusChanged(self);
        }
    }
}

-(NSString *)statusText {
    switch (self.status) {
        case kJYJMusicStatusNone:{
            return @"";
            break;
        }
        case kJYJMusicStatusRunning:{
            return @"下载中";
            break;
        }
        case kJYJMusicStatusSuspended:{
            return @"暂停下载";
            break;
        }
        case kJYJMusicStatusCompleted: {
            return @"下载完成";
            break;
        }
        case kJYJMusicStatusFailed:{
            return @"下载失败";
            break;
        }
        case kJYJMusicStatusWaiting:{
            return @"等待下载";
            break;
        }
        
    }
}
@end
